package com.doit.day01

import scala.collection.immutable
import scala.collection.mutable.ArrayBuffer

object _05_for循环 {
  def main(args: Array[String]): Unit = {

    val arr: Array[Int] = Array(1, 2, 3, 4, 5)

    val range: Range = 1 until 10

    //类似于增强for循环  by 循环步长
    for (elem <- 0 until 10 by 2) {
      //      println(elem)
    }


    //我想要打印1-9，但是不要5

    for (elem <- 1 until 10) {
      if (elem != 5) {
        println(elem)
      }
    }

    //循环守卫
    for (elem <- 1 until 10 if elem != 5) {
      println(elem)
    }

    //循环的嵌套  双重for循环
    for (i <- 1 to 9) {
      for (j <- 1 to i) {
        print(s"${i} * ${j} = ${i * j}\t")
        if (i == j) println()
      }
    }

    println("===========优雅的分隔符==========")

    for (i <- 1 to 9; j <- 1 to i) {
      print(s"${i} * ${j} = ${i * j}\t")
      if (i == j) println()
    }


    //引入变量
    for (i <- 1 to 3; j = 4 - i) {
      println("i=" + i + " j=" + j)
    }


    var j = 0
    for (i <- 1 to 3) {
      j = 4 - i
      println("i=" + i + " j=" + j)
    }

    //灵活
    //倒着打
    for(elem <- 9 to 1 by -1){
      //打 9 - 1
      println(elem)
    }

    println("==============================")
    //想把1 - 9 这9个数全部收集到数组中
    //定义一个长度为9的数组
    val buffer: ArrayBuffer[Int] = new ArrayBuffer[Int]()

    for(elem <- 1 to 9 ){
      buffer.append(elem)
    }

    println(buffer)


   val res =  for(elem <- 1 to 9) yield {
      //这个上面写什么代码都可以
      elem
    }

    println(res)

  }
}
